package com.billlv.codegenerator.service.impl;

import com.billlv.codegenerator.domain.dto.DatabaseConnectionDTO;
import com.billlv.codegenerator.domain.vo.DatabaseConnectionVO;
import com.billlv.codegenerator.domain.entity.DatabaseConnectionEntity;
import com.billlv.codegenerator.mapper.DatabaseConnectionMapper;
import com.billlv.codegenerator.repository.DatabaseConnectionRepository;
import com.billlv.codegenerator.service.DatabaseConnectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import com.billlv.codegenerator.specification.QueryCondition;
import com.billlv.codegenerator.specification.DatabaseConnectionSpecification;
import java.util.List;

@Service
public class DatabaseConnectionServiceImpl implements DatabaseConnectionService {

    @Autowired
    private DatabaseConnectionRepository databaseConnectionRepository;


    @Override
    public DatabaseConnectionVO create(DatabaseConnectionDTO dto) {
        // 使用 Mapper 转换 DTO -> Entity
        DatabaseConnectionEntity entity = DatabaseConnectionMapper.INSTANCE.toEntity(dto);

        // 保存 Entity
        DatabaseConnectionEntity savedEntity = databaseConnectionRepository.save(entity);

        // 使用 Mapper 转换 Entity -> VO
        return DatabaseConnectionMapper.INSTANCE.toVO(savedEntity);
    }

    @Override
    public DatabaseConnectionVO read(Long id) {
        // 查询 Entity
        DatabaseConnectionEntity entity = databaseConnectionRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("DatabaseConnection not found with ID: " + id));

        // 使用 Mapper 转换 Entity -> VO
        return DatabaseConnectionMapper.INSTANCE.toVO(entity);
    }

    @Override
    public DatabaseConnectionVO update(Long id, DatabaseConnectionDTO dto) {
        // 查询 Entity
        DatabaseConnectionEntity entity = databaseConnectionRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("DatabaseConnection not found with ID: " + id));

        // 使用 Mapper 更新 Entity
        DatabaseConnectionMapper.INSTANCE.updateEntityFromDTO(dto, entity);

        // 保存更新后的 Entity
        DatabaseConnectionEntity updatedEntity = databaseConnectionRepository.save(entity);

        // 使用 Mapper 转换 Entity -> VO
        return DatabaseConnectionMapper.INSTANCE.toVO(updatedEntity);
    }

    @Override
    public void delete(Long id) {
        if (!databaseConnectionRepository.existsById(id)) {
            throw new RuntimeException("DatabaseConnection not found with ID: " + id);
        }
        databaseConnectionRepository.deleteById(id);
    }

    @Override
    public Page<DatabaseConnectionVO> getAll(   Pageable pageable) {
        // 查询分页数据
        Page<DatabaseConnectionEntity> entities = databaseConnectionRepository.findAll(pageable);

        // 使用 Mapper 转换 Page<Entity> -> Page<VO>
        return entities.map(DatabaseConnectionMapper.INSTANCE::toVO);
    }
     @Override
     public Page<DatabaseConnectionVO> search(List<QueryCondition> conditions, Pageable pageable) {
            return databaseConnectionRepository
                    .findAll(DatabaseConnectionSpecification.build(conditions), pageable)
                    .map(DatabaseConnectionMapper.INSTANCE::toVO);
        }
}
